package CourseSelectionSystem;

import java.util.ArrayList;
import java.util.List;

public class Student extends Person {
    private List<Course> selectedCourses;

    public Student(String id, String name, String gender) {
        super(id, name, gender);
        this.selectedCourses = new ArrayList<>();
    }

    // 添加计算总学分的方法
    public int getTotalCredits() {
        int total = 0;
        for (Course course : selectedCourses) {
            total += course.getCredits();
        }
        return total;
    }

    // 修改打印课表方法，添加学分显示
    public void printCourseSchedule() {
        System.out.println("\n📚 " + getName() + " 的课程表");
        System.out.println("基本信息: " + getBasicInfo());
        System.out.println("=" + "=".repeat(90));

        if (selectedCourses.isEmpty()) {
            System.out.println("   暂无课程");
        } else {
            System.out.printf("%-12s %-15s %-10s %-15s %-20s %-6s%n",
                    "课程编号", "课程名称", "授课教师", "上课地点", "上课时间", "学分");
            System.out.println("-".repeat(90));

            for (Course course : selectedCourses) {
                String teacherName = (course.getTeacher() != null) ? course.getTeacher().getName() : "未分配";
                System.out.printf("%-12s %-15s %-10s %-15s %-20s %-6d%n",
                        course.getCourseId(),
                        course.getCourseName(),
                        teacherName,
                        course.getLocation(),
                        course.getTime(),
                        course.getCredits());
            }

            // 显示总学分
            System.out.println("-".repeat(90));
            System.out.printf("%-72s %-6s%n", "总学分:", getTotalCredits());
        }
        System.out.println("=" + "=".repeat(90));
    }

    // 原有方法保持不变...
    public boolean selectCourse(Course course) {
        for (Course selected : selectedCourses) {
            if (selected.getCourseId().equals(course.getCourseId())) {
                System.out.println("❌ 您已经选择了这门课程！");
                return false;
            }
        }

        for (Course selected : selectedCourses) {
            if (selected.getTime().equals(course.getTime())) {
                System.out.println("❌ 时间冲突！您已经在 " + selected.getTime() + " 安排了 " + selected.getCourseName());
                return false;
            }
        }

        if (course.isFull()) {
            System.out.println("❌ 课程已满，无法选择！");
            return false;
        }

        selectedCourses.add(course);
        course.enrollStudent();
        return true;
    }

    public boolean dropCourse(String courseId) {
        for (int i = 0; i < selectedCourses.size(); i++) {
            Course course = selectedCourses.get(i);
            if (course.getCourseId().equals(courseId)) {
                selectedCourses.remove(i);
                course.dropStudent();
                return true;
            }
        }
        return false;
    }

    public List<Course> getSelectedCourses() {
        return selectedCourses;
    }

    public boolean hasCourse(String courseId) {
        for (Course course : selectedCourses) {
            if (course.getCourseId().equals(courseId)) {
                return true;
            }
        }
        return false;
    }
}